package com.taiji.dianthus.service.app.impl;

import com.taiji.dianthus.common.BusinessMessage;
import com.taiji.dianthus.dao.app.AppInfoRepository;
import com.taiji.dianthus.domain.app.AppInfo;
import com.taiji.dianthus.domain.sys.DSysUser;
import com.taiji.dianthus.service.app.AppInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @ClassName SysLogServiceImpl
 * @Description
 * @Author H.M
 * @Date 2020/6/22
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class AppInfoServiceImpl implements AppInfoService {

    @Autowired
    private AppInfoRepository repository;


    /**
     * 查询注册应用列表
     *
     * @param pageSize
     * @param currentPage * @return
     */
    @Override
    public BusinessMessage list(Integer pageSize, Integer currentPage, String appName, Integer enableFlag, DSysUser currentUser) {
        BusinessMessage message = new BusinessMessage();
        try {
            Sort sort = new Sort(Sort.Direction.DESC, "createTime");
            //分页参数
            Pageable pageable = PageRequest.of(currentPage, pageSize, sort);
            Page<AppInfo> logPage;
            //创建查询参数
            Specification<AppInfo> spec = (Root<AppInfo> root, CriteriaQuery<?> query, CriteriaBuilder cb) -> {
                List<Predicate> pl = new ArrayList<Predicate>();
                if (!StringUtils.isEmpty(appName)) {
                    pl.add(cb.like(root.<String>get("appName"), "%" + appName + "%"));
                }
                pl.add(cb.equal(root.<String>get("enableFlag"), 1));

                return cb.and(pl.toArray(new Predicate[0]));
            };
            logPage = repository.findAll(spec, pageable);
            message.setMsg("查询注册应用列表成功");
            message.setData(logPage);
            message.setSuccess(true);
        } catch (Exception e) {
            e.printStackTrace();
            message.setMsg("查询注册应用列表失败");
        } finally {
            return message;
        }

    }

    @Override
    public AppInfo getAppInfoById(String id) {
        return repository.getOne(id);
    }

    @Override
    public BusinessMessage saveAppInfo(AppInfo appInfo, DSysUser currentUser) {
        BusinessMessage message = new BusinessMessage();
        appInfo.setEnableFlag(1);
        try {
            if (StringUtils.isEmpty(appInfo.getId())) {
                appInfo.setCreateTime(new Date());
                appInfo.setCreateByUserId(currentUser.getId());
                appInfo.setCreateByUserName(currentUser.getName());
            } else {
                AppInfo before = repository.getOne(appInfo.getId());
                appInfo.setCreateTime(before.getCreateTime());
                appInfo.setCreateByUserName(before.getCreateByUserName());
                appInfo.setCreateByUserId(before.getCreateByUserId());
                appInfo.setEditTime(new Date());
                appInfo.setEditByUserId(currentUser.getId());
                appInfo.setEditByUserName(currentUser.getName());
            }
            repository.saveAndFlush(appInfo);
            message.setSuccess(true);
            message.setMsg("保存成功");
        } catch (Exception e) {
            e.printStackTrace();
            message.setMsg("系统异常，保存失败");
        }
        return message;
    }

    @Override
    public BusinessMessage deleteById(String id) {
        BusinessMessage message = new BusinessMessage();
        try {
            repository.updateFlag(id);
            message.setSuccess(true);
            message.setMsg("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            message.setMsg("系统异常，删除失败");
        }
        return message;
    }

    @Override
    public BusinessMessage checkAppName(String appName, String id) {
        BusinessMessage message = new BusinessMessage();
        List<AppInfo> list = null;
        if (StringUtils.isEmpty(id)) {
            list = repository.getByAppNameByAdd(appName);
        } else {
            list = repository.getByAppNameByEdit(appName, id);
        }
        if (list != null && list.size() > 0) {
            message.setSuccess(true);
        }
        return message;
    }

    @Override
    public AppInfo getAppInfoByAppName(String appName) {
        return repository.getAppInfoByAppName(appName);
    }

    @Override
    public AppInfo getAppInfoByAppId(String appId) {
        return repository.getAppInfoByAppId(appId);
    }


}
